package com.mobile.challenge.web;
import com.mobile.challenge.core.Result;
import com.mobile.challenge.core.ResultGenerator;
import com.mobile.challenge.model.SignUpRecord;
import com.mobile.challenge.model.User;
import com.mobile.challenge.service.SignUpRecordService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.mobile.challenge.service.UserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
* Created by CodeGenerator on 2018/06/08.
*/
@RestController
@RequestMapping("/signUp")
@Api(value = "报名接口",tags = {"报名记录管理"})
public class SignUpRecordController {
    @Resource
    private UserService userService;
    @Resource
    private SignUpRecordService signUpRecordService;
    /**
     * 是否已报名
     * @param user
     * @return
     */
    @RequestMapping(value = "/ifOldHand", method = RequestMethod.POST, produces = {"application/json"})
    @ResponseStatus(HttpStatus.OK)
    @ApiOperation(value = "是否已报名", notes = "必须提供一个phoneNo")
    public Result getRemCount4Challenge(@RequestBody SignUpRecord user) {
        //是否已报名
        Map data = signUpRecordService.findStatusByPhoneNo(user.getPhoneNo());

        return ResultGenerator.genSuccessResult(data);
    }

    /**
     * 报名(带锁)
     * @param user
     * @return
     */
    @RequestMapping(value = "/add", method = RequestMethod.POST, produces = {"application/json"})
    @ResponseStatus(HttpStatus.OK)
    @ApiOperation(value = "报名", notes = "必须提供一个phoneNo,进入活动初始化页面时请求此接口")
    public synchronized Result add(@RequestBody SignUpRecord user) {
        Map data = new HashMap();
        //报名
        Map rs = signUpRecordService.signUp(user);
        if(!(boolean)rs.get("code")){
            return ResultGenerator.genFailResult((String) rs.get("msg"),data);
        }
        return ResultGenerator.genSuccessResult(data);
    }
}
